from conan import ConanFile
from conan.tools.cmake import cmake_layout
from conan.tools.files import copy, export_conandata_patches, get, rmdir, chdir, replace_in_file
from conan.tools.microsoft import check_min_vs, is_msvc
import os
from conan.tools.gnu import Autotools, AutotoolsToolchain
required_conan_version = ">=1.53.0"


class LibxmpConan(ConanFile):
    name = "libxmp"
    description = "Libxmp is a library that renders module files to PCM data. "
    # topics = ("speex", "audio", "decoder", "decoding", "multimedia", "sound")
    url = "https://github.com/conan-io/conan-center-index"
    homepage = "https://github.com/libxmp/libxmp"

    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    def export_sources(self):
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")
        self.settings.rm_safe("compiler.cppstd")
        self.settings.rm_safe("compiler.libcxx")

    def layout(self):
        cmake_layout(self, src_folder="src")

    def validate(self):
        check_min_vs(self, 190)

    def source(self):
        get(self, **self.conan_data["sources"][self.version],
            destination=self.source_folder, strip_root=True)

    @property
    def _settings_build(self):
        return getattr(self, "settings_build", self.settings)

    def build_requirements(self):
        self.tool_requires("libtool/2.4.7")
        if self._settings_build.os == "Windows" and not is_msvc(self):
            self.win_bash = True
            if not self.conf.get("tools.microsoft.bash:path", check_type=str):
                self.tool_requires("msys2/cci.latest")

    def generate(self):
        tc = AutotoolsToolchain(self)
        tc.generate()


    def build(self):
        replace_in_file(self, os.path.join(self.source_folder, "Makefile.in"), "-c @CFLAGS@ @DEFS@ -D_REENTRANT", "-c -O3 @CFLAGS@ @DEFS@ -D_REENTRANT")
        with chdir(self, self.source_folder):
            autotools = Autotools(self)
            autotools.autoreconf()
            autotools.configure()
            autotools.make()

    def package(self):
        copy(self, "COPYING", src=self.source_folder, dst=os.path.join(self.package_folder, "licenses"))
        with chdir(self, self.source_folder):
            autotools = Autotools(self)
            autotools.install()

        rmdir(self, os.path.join(self.package_folder, "lib", "pkgconfig"))
        rmdir(self, os.path.join(self.package_folder, "lib", "cmake"))

    def package_info(self):
        self.cpp_info.set_property("cmake_file_name", "xmp")
        self.cpp_info.set_property("cmake_target_name", "xmp")
        self.cpp_info.set_property("pkg_config_name", "xmp")
        self.cpp_info.libs=['xmp']

